package com.powernode.teatraceability.controller.image;


import com.powernode.teatraceability.service.basicinfomgt.ProductService;
import com.powernode.teatraceability.utils.JsonData;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.resource.ResourceResolverChain;
import org.springframework.web.servlet.resource.ResourceUrlProvider;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Base64;

@RestController
@RequestMapping("/*/image")
public class ImageController {

/*    @GetMapping("/{base64Str}")
    public void getImage(@PathVariable String base64Str,
                         HttpServletResponse response) throws IOException {
        final byte[] imageBytes = Base64.getDecoder().decode(base64Str);
        response.setContentType("image/jpeg;image/png");
        final ServletOutputStream out = response.getOutputStream();
        out.write(imageBytes);

        out.flush();
        out.close();
    }*/


    @PostMapping
    public JsonData setImage(@RequestParam("file") MultipartFile file) throws IOException {
        final byte[] bytes = file.getBytes();
        final String imageStr = Base64.getEncoder().encodeToString(bytes);
        final JsonData<Object> jsonData = JsonData.builder()
                .msg("上传成功")
                .status(200)
                .data(imageStr)
                .build();
        return jsonData;
    }

    /*@GetMapping
    public void getImage(@RequestParam String path,
                         HttpServletResponse response) throws FileNotFoundException {
        InputStream in = new FileInputStream(new File("images"+ path));
        System.out.println(in);
        if (path.endsWith(".jpg")) {
            response.setContentType("image/jpg");
        } else if (path.endsWith(".png")) {
            response.setContentType("image/png");
        }
        if(in != null){
            try {
                int totalBytes = in.available();
                byte[] bytes = new byte[totalBytes];
                in.read(bytes);
                in.close();
                ServletOutputStream outputStream = response.getOutputStream();
                outputStream.write(bytes);
                outputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }*/

/*    @PostMapping("/{dirName}/{picName}")
    public String setImage(MultipartFile file,
                           @PathVariable String dirName,
                           @PathVariable String picName,
                           ResourceUrlProvider resourceUrlProvider){
        InputStream in = null;
        OutputStream out = null;
        try {
            in = file.getInputStream();
            out = new FileOutputStream("src/main/resources/static/images/" + dirName + "/" + picName);
            byte[] bytes = new byte[1024 * 100];// 100kb
            int cnt = 0;
            while ((cnt = in.read(bytes)) != -1) {
                out.write(bytes, 0, cnt);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if (in!=null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        resourceUrlProvider.getForLookupPath("/");
        return "/"+dirName+"/" + file.getName();
    }*/
/*@PostMapping("/{dirName}/{picName}")
public String setImage(MultipartFile file,
                       @PathVariable String dirName,
                       @PathVariable String picName) {
    try (InputStream in = file.getInputStream();
         OutputStream out = new FileOutputStream("images/" + dirName + "/" + picName)) {

        byte[] bytes = new byte[1024 * 100]; // 100kb
        int cnt = 0;
        while ((cnt = in.read(bytes)) != -1) {
            out.write(bytes, 0, cnt);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "/" + dirName + "/" + picName;
    }*/
}
